package com.xuelang.pipeline.domain.resource.service.impl;

import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.resource.dto.ClusterQuery;
import com.xuelang.pipeline.domain.resource.Cluster;
import com.xuelang.pipeline.domain.resource.repository.ClusterRepository;
import com.xuelang.pipeline.domain.resource.service.ClusterDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 集群领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class ClusterDomainServiceImpl implements ClusterDomainService {

    @Autowired
    private ClusterRepository clusterRepository;

    @Override
    public PageRecord<Cluster> pageList(long currentPage, long pageSize, ClusterQuery clusterQuery) {
        return clusterRepository.pageList(currentPage, pageSize, clusterQuery);
    }

    @Override
    public List<Cluster> list(ClusterQuery clusterQuery) {
        return clusterRepository.list(clusterQuery);
    }


    @Override
    public Boolean save(Cluster cluster) {
        if(existsName(cluster.getUuid(), cluster.getName())) {
            throw new BizException("name已经存在");
        }
        if(existsAlias(cluster.getUuid(), cluster.getAlias())) {
            throw new BizException("alias已经存在");
        }
        return clusterRepository.save(cluster);
    }

    @Override
    public Boolean update(Cluster cluster) {
        // 更新时，名称不能重复，摒弃当前环境的名称。
        if(existsName(cluster.getUuid(), cluster.getName())) {
            throw new BizException("name已经存在");
        }
        if(existsName(cluster.getUuid(), cluster.getAlias())) {
            throw new BizException("alias已经存在");
        }
        return clusterRepository.update(cluster);
    }

    @Override
    public Boolean delete(String clusterUuid) {
        return clusterRepository.delete(clusterUuid);
    }

    @Override
    public Boolean existsAlias(String clusterUuid, String clusterAlias) {
        return clusterRepository.existsAlias(clusterUuid, clusterAlias);
    }

    @Override
    public Boolean existsName(String clusterUuid, String clusterName) {
        return clusterRepository.existsName(clusterUuid, clusterName);
    }

    @Override
    public Boolean existsCluster(String custerUuid) {
        return clusterRepository.existsCluster(custerUuid);
    }

    @Override
    public Cluster query(String clusterUuid) {
        return clusterRepository.query(clusterUuid);
    }

}
